package com.rp.common.utils;

import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.RandomUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 类说明:
 *
 * @author ****
 * @date 2020-07-11 16:55  新建
 */

public class UploadFile {

    public static boolean handleFileUpload(MultipartFile file,String filePath) {
        if (file.isEmpty()) {
            System.out.println("文件为空");
            return false;
        }
        // 获取文件名
        String fileName = file.getOriginalFilename();
        System.out.println("上传的文件名为：" + fileName);
        // 获取文件的后缀名
        String suffixName = fileName.substring(fileName.lastIndexOf("."));
        System.out.println("上传的后缀名为：" + suffixName);
        // 文件上传后的路径
        File dest = new File(filePath + fileName);
        // 检测是否存在目录
        if (!dest.getParentFile().exists()) {
            dest.getParentFile().mkdirs();
        }
        try {
            file.transferTo(dest);
            System.out.println("上传成功");
            return true;
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("上传失败");
        return false;
    }

    /**
     * 多文件具体上传时间，主要是使用了MultipartHttpServletRequest和MultipartFile
     *
     *
     * @return
     */

    public static boolean handleFileUploads(MultipartFile[] files,String filePath) {
        List<String> list = new ArrayList<>();
        MultipartFile file = null;
        for (int i = 0; i < files.length; ++i) {
            file = files[i];
            // 获取文件名
            String fileName = file.getOriginalFilename();
            System.out.println("上传的文件名为：" + fileName);
            if (!file.isEmpty()) {
                list.add(filePath + fileName);
                File dest = new File(filePath + fileName);
                // 检测是否存在目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                } catch (Exception e) {
                    System.out.println("上传失败 " + i + " => " + e.getMessage());
                    return false;
                }
            } else {
                System.out.println("上传失败 " + i + " 文件为空.");
                return false;
            }
        }
        return true;
    }

    //返回上传文件的路径
    public static List<String> handleFileUploads(MultipartFile[] files,String filePath,String readurl) {
        List<String> list = new ArrayList<>();
        MultipartFile file = null;
        for (int i = 0; i < files.length; ++i) {
            file = files[i];
            // 获取文件名
            String fileName = file.getOriginalFilename();
            System.out.println("上传的文件名为：" + fileName);
            String uuid = RandomUtil.randomString(16);
            if (!file.isEmpty()) {
                list.add(readurl + "/" +  uuid);
                File dest = new File(filePath + "/" + uuid);

                // 检测是否存在目录
                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdirs();
                }
                try {
                    file.transferTo(dest);
                    System.out.println(filePath + "/" + fileName);
                } catch (Exception e) {
                    System.out.println("上传失败 " + i + " => " + e.getMessage());
                }
            } else {
                System.out.println("上传失败 " + i + " 文件为空.");
            }
        }
        return list;
    }
}
